package service;

import dao.DBTestFactory;
import dao.FlowerDAO;
import dao.FlowerStoreDAO;
import dao.OrdersDAO;
import entity.Flower;
import entity.FlowerStore;
import entity.Orders;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FlowerStoreImp implements FlowerStoreService{
    @Override
    public boolean addFlower(Flower flower) {     //培育新品种
        //从ui传入flower，Flower表执行插入
        FlowerDAO fd = DBTestFactory.getInstance().getFlowerDAO();
        List<Flower> list = fd.addFlower(flower);
        //更新FlowerStore数据
        FlowerStoreDAO fsd = DBTestFactory.getInstance().getFlowerStoreDAO();
        boolean fsdadd = fsd.addFlower();

        if (fsdadd && !list.isEmpty()){
            return true;    //list不为空且商店数据更新成功则鲜花培育成功
        }
        return false;
    }

    @Override
    public HashMap<String, Integer> checkStore() {    //查看所有库存
        HashMap<String,Integer> hashMap = new HashMap<String,Integer>();
        FlowerDAO fd = DBTestFactory.getInstance().getFlowerDAO();
        List<Flower> list = fd.checkFlower();
        for (Flower flower:list){     //品种和对应库存添加到hashMap中
            hashMap.get(flower.getVariety());
            hashMap.values().add(flower.getStore());
        }
        return hashMap;     //返回数据到ui层
    }

    @Override
    public List<Orders> checkOrders() {        //查询订单
        List<Orders> list = new ArrayList<Orders>();
        OrdersDAO od = DBTestFactory.getInstance().getOrdersDAO();
        list=od.checkOrders();
        return list;
    }

    @Override
    public List<String> checkStatus() {        //查看销售情况
        List<String> list = new ArrayList<>();
        FlowerStoreDAO fsd = DBTestFactory.getInstance().getFlowerStoreDAO();
        List<FlowerStore> fsdlist = new ArrayList<FlowerStore>();
        fsdlist = fsd.checkFlowerStore();
        String s1="";  //销量
        String s2="";  //销售额
        for (FlowerStore flowerStore : fsdlist){
            list.add(String.valueOf(flowerStore.getAmount()));
            list.add(String.valueOf(flowerStore.getAcount()));
        }
        return list;
    }

    @Override
    public boolean inFlower(String vartety,int num) {
        FlowerDAO fd = DBTestFactory.getInstance().getFlowerDAO();
        int nums = fd.inFlower(vartety,num);
        //匹配：ui层传入的num和DAO层返回的nums相等，入库成功
        return num==nums;
    }

    @Override
    public boolean outFlower(String vartety,int num) {
        FlowerDAO fd = DBTestFactory.getInstance().getFlowerDAO();
        int nums = fd.outFlower(vartety,num);
        //匹配：ui层传入的num和DAO层返回的nums相等，出库成功
        return num==nums;
    }
}
